PRO L8CALIBRATE,File,outputfile,bilFile
  COMPILE_OPT IDL2
  ; Start the application
  e = ENVI(/HEADLESS)

  ; A Landsat 8 OLI dataset consists of one TIFF file per band,
  ; with an associated metadata file (*_MTL.txt). Open the
  ; metadata file to automatically read the gains and offsets.

;  File = File_Search('F:\LC08_L1TP_129026_20181128_20181211_01_T1', '*_MTL.txt')
;  outputfile="F:\g11.dat"
;  bilFile="F:\1122.dat"
  
  File = File_Search('D:\Lugz\LC81210402020106LGN00', '*_MTL.txt')
  File = 'D:\Lugz\LC81210402020106LGN00\LC08_L1TP_121040_20200415_20200423_01_T1_MTL.txt'
  Raster = e.OpenRaster(File)

  ; Get the radiometric calibration task from the catalog of ENVI tasks.
  Task = ENVITask('RadiometricCalibration')

  ; Define inputs. Since radiance is the default calibration method
  ; you do not need to specify it here.
  Task.Input_Raster = Raster[0] ; Bands 1-7
  Task.Output_Data_Type = 'Float'

  ; Define output raster URI
  Task.Output_Raster_URI = outputfile
  
  Task.SCALE_FACTOR =0.1

  ; Run the task
  Task.Execute
  
  Task.OUTPUT_RASTER.Export, bilFile, 'envi', INTERLEAVE='bil'

;  ; Get the data collection
;  DataColl = e.Data
;
;  ; Add the output to the data collection
;  DataColl.Add, Task.Output_Raster

  ; Close the ENVI session
  e.Close
END